package net.vinote.smart.platform.service.dw.html;

import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.service.DWException;
import net.vinote.smart.platform.service.awe.layout.easyui.DateBox;
import net.vinote.smart.platform.service.awe.layout.easyui.DateTimeBox;
import net.vinote.smart.platform.service.awe.layout.easyui.TimeSpinner;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 标签显示的html代码
 */
public class HtmlTag extends HtmlTemplate implements Cloneable {

	private String tagName;

	private boolean isReadOnly;

	private boolean isFilter;// 是否查询

	private boolean isRequired;// 是否必须

	private boolean isVisible;// 是否可见

	private String afterField;

	private String showTag;

	private String colField;

	private String htmlSource;

	private String htmlVal;

	private String dataObj;

	private String align;// 对齐

	private String value;

	private int colSpan;

	private int rowSpan;

	private int showStyle;// 1:Freeform,2:Gird

	public HtmlTag() {
	}

	public HtmlTag(int showStyle) {
		this.showStyle = showStyle;
	}

	protected HtmlTag(HtmlTag tag) {
		setTagName(tag.tagName);
		setIsReadOnly(tag.isReadOnly);
		setAfterField(tag.afterField);
		setShowTag(tag.showTag);
		setHtmlSource(tag.htmlSource);
		setHtmlVal(tag.htmlVal);
		setColField(tag.colField);
		setDataObj(tag.dataObj);
		showStyle = tag.showStyle;
		setAlign(tag.align);
		setIsRequired(tag.isRequired);
		setIsFilter(tag.isFilter);
		setVisible(tag.isVisible);
		setAttributes(tag.attributes);
		setColSpan(tag.colSpan);
		setRowSpan(tag.rowSpan);
	}

	@Override
	protected String getHtmlTemplate() {
		return null;
	}

	@Override
	protected void initPlaceHolder() {
	}

	public String getName() {
		return getAttribute("name");
	}

	public void setName(String name) {
		setAttribute("name", name);
	}

	public String getId() {
		return getAttribute("id");
	}

	public void setId(String id) {
		setAttribute("id", id);
	}

	public void setIsReadOnly(boolean isReadOnly) {
		if (isReadOnly) {
			setAttribute("readonly", "readonly");
		} else {
			removeAttribute("readonly");
		}
	}

	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
		if (!isVisible) {
			addStyle("display:none;");
		}
	}

	public boolean isVisible() {
		return isVisible;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getTagName() {
		return tagName;
	}

	public String getAfterField() {
		return afterField;
	}

	public void setAfterField(String afterField) {
		this.afterField = afterField == null ? "" : afterField;
	}

	public String getShowTag() {
		return showTag;
	}

	public void setShowTag(String showTag) {
		this.showTag = showTag;
	}

	public String getColField() {
		return colField;
	}

	public void setColField(String colField) {
		this.colField = colField;
	}

	public String getHtmlSource() {
		return htmlSource;
	}

	public void setHtmlSource(String htmlSource) {
		this.htmlSource = htmlSource;
	}

	public String getHtmlVal() {
		return htmlVal;
	}

	public void setHtmlVal(String htmlVal) {
		this.htmlVal = htmlVal;
	}

	public String getDataObj() {
		return dataObj;
	}

	public void setDataObj(String dataObj) {
		this.dataObj = dataObj;
	}

	public void setClass(String clazz) {
		setAttribute("class", clazz);
	}

	public void addClass(String clazz) {
		setAttribute(
				"class",
				(StringFunction.nullToBlank(getAttribute("class")) + " " + clazz)
						.trim());
	}

	// 追加样式
	public void addStyle(String style) {
		String style0 = getAttribute("style");
		if (style0 == null) {
			setAttribute("style", style);
		} else {
			setAttribute("style", style0 + style);
		}
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public HtmlTag clone() {
		HtmlTag o = null;
		try {
			o = (HtmlTag) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}

	public void setIsFilter(boolean filter) {
		this.isFilter = filter;
	}

	public void setIsRequired(boolean required) {
		this.isRequired = required;
		// 添加required样式
		if (required) {
			addClass("required");
		}
	}

	public int getColSpan() {
		return colSpan < 0 ? 1 : colSpan;
	}

	public void setColSpan(int colSpan) {
		this.colSpan = colSpan;
	}

	public int getRowSpan() {
		return rowSpan;
	}

	public void setRowSpan(int rowSpan) {
		this.rowSpan = rowSpan;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public boolean isFilter() {
		return isFilter;
	}

	public HtmlTag getTagInstance() {
		HtmlTag tag;
		if (showTag == null || "".equals(showTag.trim())) {
			showTag = "span";
		}
		if (showTag.startsWith("input")) {
			String type = showTag.substring(showTag.indexOf("[") + 1,
					showTag.indexOf("]"));
			if ("radio".equals(type)) {// 单选框
				tag = new InputRadioGroupTag(this);
			} else if ("password".equals(type)) {
				tag = new InputPasswordTag(this);
			} else if ("checkbox".equals(type)) {
				tag = new InputCheckBoxGroupTag(this);
			} else if ("date".equals(type)) {
				tag = new DateBox(this);
			} else if ("time".equals(type)) {
				tag = new TimeSpinner(this);
			} else if ("datetime".equals(type)) {
				tag = new DateTimeBox(this);
			} else {
				tag = new InputTextTag(this);// 默认为单行文本框
			}
			tag.addClass("easyui-validatebox");
		} else if (showTag.startsWith("span")) {
			tag = new SpanTag(this);
		} else if (showTag.equals("select")) {
			tag = new SelectTag(this);
			tag.addClass("easyui-combobox");
		} else if (showTag.equals("textarea")) {
			tag = new TextAreaTag(this);
		} else if (showTag.equals("richTextArea")) {
			tag = new RichTextAreaTag(this);
		} else if (showTag.equals("image")) {
			tag = new ImageTag(this);
		} else {
			throw new DWException("未知标签:" + showTag);
		}
		return tag;
	}

	public boolean isRequired() {
		return isRequired;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

}
